package com.jydd.plug_in_study;

import android.Manifest;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;

import java.util.ArrayList;

import androidx.core.app.ActivityCompat;

/**
 * created by 韦敏敏
 * on 2019/12/26
 */
public class RequestPermissionUtils {
    private Context mContext = null;

    public RequestPermissionUtils(Context context) {
        this.mContext = context;
    }
    public void requestPermissions(int requestCode){
        // 安卓6.0系统 --> API23
        if(Build.VERSION.SDK_INT >= 23){
            ArrayList<String> requestPerssionArr = new ArrayList<>();
            int hasSdcardRead = mContext.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);
            if(hasSdcardRead != PackageManager.PERMISSION_GRANTED){
                requestPerssionArr.add(Manifest.permission.READ_EXTERNAL_STORAGE);
            }
            int hasSdcardWrite = mContext.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            if(hasSdcardWrite != PackageManager.PERMISSION_GRANTED){
                requestPerssionArr.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            }
            // 是否应该显示权限请求
            if(!requestPerssionArr.isEmpty()){
                String[] requestArray = new String[requestPerssionArr.size()];
                for(int i = 0; i < requestArray.length; i++){
                    requestArray[i] = requestPerssionArr.get(i);
                }
                ActivityCompat.requestPermissions((Activity) mContext, requestArray, requestCode);
            }
        }
    }
}
